package com.activiti.z_six.controller;

import com.activiti.z_six.service.ProcessDefinitionService;
import com.activiti.z_six.util.AjaxResponse;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;

@RestController
public class ProcessDefinitionController {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    ProcessDefinitionService processDefinitionService;
    /*
       获取流程分类
     */
    @GetMapping("/deployment/getFlowSort")
    public AjaxResponse getFlowSort(HttpServletResponse response,
                                    @RequestParam("page") Integer page,
                                    @RequestParam("pagesize") Integer pagesize){
        int startIndex=(page-1)*pagesize;
        int maxIndex=startIndex+pagesize;
        return processDefinitionService.getFlowSort(startIndex,maxIndex);
    }
    /*
       获取流程分类
     */
    @GetMapping("/deployment/getFlowSortAll")
    public AjaxResponse getFlowSortAll(){
        return processDefinitionService.getFlowSortAll();
    }
    /*
       获取流程分类
     */
    @GetMapping("/deployment/getFlowSortTree")
    public AjaxResponse getFlowSortTree(){
        return processDefinitionService.getFlowSortTree();
    }
    /**
    获取流程列表
     */
    @PostMapping("/deployment/getDeployment")
    public AjaxResponse getDeployment(@RequestBody JSONObject param){
        Integer page=param.getInteger("page");
        Integer pagesize=param.getInteger("pagesize");
        String sortid= param.getString("sortid");
        String name=param.getString("name");
        return processDefinitionService.getDeployment(page,pagesize,sortid,name);
    }
    /*
    发布流程
     */
    @PostMapping(value = "/deployment/deployWithBPMNJS")
    public AjaxResponse deployWithBPMNJS(@RequestBody JSONObject param){
        String stringBPMNXml=param.get("stringBPMNXml").toString();
        return processDefinitionService.deployWithBPMNJS(stringBPMNXml);
    }
    /*
    获取流程定义xml
     */
    @GetMapping(value="/deployment/getDeploymentXmlById")
    public void getDeploymentXmlById(HttpServletResponse response,
                                     @RequestParam("deploymentId") String deploymentId){
        try {
            InputStream inputStream = repositoryService.getResourceAsStream(deploymentId,"createWithBPMNJS.bpmn");
            int count = inputStream.available();
            byte[] bytes = new byte[count];
            response.setContentType("text/xml");
            OutputStream outputStream = response.getOutputStream();
            while (inputStream.read(bytes) != -1) {
                outputStream.write(bytes);
            }
            inputStream.close();
        } catch (Exception e) {
            e.toString();
        }
    }
    @GetMapping(value="/deployment/getSatrtPageData")
    public AjaxResponse getSatrtPageData(){
        return processDefinitionService.getSatrtPageData();
    }
    @GetMapping(value="/deployment/getFlowEnum")
    public AjaxResponse getFlowEnum(){
        try{
            HashMap<String,Object> hashMap=processDefinitionService.getFlowEnum();
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.getMessage());
        }
    }
    /*
    更新业务流程配置
     */
    @PostMapping(value = "/deployment/updateDeployment")
    public AjaxResponse updateDeployment(@RequestBody JSONObject param){
        String deploymentId=param.getString("id");
        String process_key=param.getString("process_key");
        String name=param.getString("name");
        String flowSort=param.getString("sortid");
        String form_type=param.getString("form_type");
        String formMap=param.getString("formMap");
        String form_url=param.getString("form_url");
        String mapJson=param.getString("mapJson");
        return processDefinitionService.updateDeployment(process_key,deploymentId,flowSort,name,form_type
                ,formMap,form_url,mapJson);
    }
    /*
   增加流程分类
    */
    @PostMapping(value = "/deployment/addFlowSort")
    public AjaxResponse addFlowSort(@RequestBody JSONObject param){
        String name=param.get("name").toString();
        Long parentid=Long.parseLong("0");
        if(!SystemConfig.IsNullOrEmpty(param.getString("parentId"))){
            parentid=param.getLong("parentId");
        }
        return processDefinitionService.addFlowSort(name,parentid);
    }
    /*
   修改流程分类
    */
    @PostMapping(value = "/deployment/saveFlowSort")
    public AjaxResponse saveFlowSort(@RequestBody JSONObject param){
        Long id=param.getLong("id");
        String name=param.get("name").toString();
        Long parentid=param.getLong("parentId");
        return processDefinitionService.saveFlowSort(id,name,parentid);
    }
    /*
  删除流程分类
   */
    @PostMapping(value = "/deployment/deleteFlowSort")
    public AjaxResponse deleteFlowSort(@RequestBody JSONObject param){
        Long id=param.getLong("id");
        String name=param.get("name").toString();
        Long parentid=param.getLong("parentId");
        return processDefinitionService.deleteFlowSort(id,name,parentid);
    }

    /**
     * 删除流程定义
     * @param response
     * @param deploymentId
     * @return
     */
    @GetMapping("deployment/delDefinition")
    public AjaxResponse delDefinition(HttpServletResponse response,
                                      @RequestParam("depID") String deploymentId){
        return processDefinitionService.deleteDefinition(deploymentId);
    }
}
